package com.jrx.anytxn.param.controller.account;

import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.account.SubjectTableReq;
import com.jrx.anytxn.param.dto.account.SubjectTableRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.account.ISubjectTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @author by yf
 * @Classname SubjectTableController
 * @Description 科目表管理
 * @Date 2019/7/5 0005 下午 14:01
 */
@Api(tags = "科目表相关页面api")
@RestController
@RequestMapping("/param")
public class SubjectTableController {

    private static final Logger logger = LoggerFactory.getLogger(SubjectTableController.class);

    @Autowired
    private ISubjectTableService iSubjectTableService;
    @Autowired
    private InfoService infoService;

    @ApiOperation("根据租户ID分页查询科目信息")
    @GetMapping("/subjectTables/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<SubjectTableRes>> getAllSubjectsByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize,@RequestParam("subjectId") String subjectId) {
        TxnRespResult<TxnPage<SubjectTableRes>> result = new TxnRespResult<>();
        TxnPage<SubjectTableRes> res;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            res = iSubjectTableService.getAllPageByTenId(pageNum, pageSize, tenantId,subjectId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("通过科目表ID和租户Id查询信息")
    @GetMapping("/subjectTables/subjectId/{subjectId}/tenantId/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subjectId", value = "科目号", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true)})
    public TxnRespResult<SubjectTableRes> getBySubjectIdAndOrgId(@PathVariable(value = "subjectId") String subjectId, @PathVariable("tenantId") String tenantId) {
        TxnRespResult<SubjectTableRes> result = new TxnRespResult<>();
        SubjectTableRes res;
        try {
            res = iSubjectTableService.getByTenIdIdAndSubjectId(subjectId, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("增加科目信息")
    @PostMapping("/subjectTables")
    public TxnRespResult<SubjectTableRes> createSubjectTable(@RequestBody SubjectTableReq subjectTableReq) {
        TxnRespResult<SubjectTableRes> result = new TxnRespResult<>();
        SubjectTableRes res;
        try {
            subjectTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            subjectTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = iSubjectTableService.addSubjectTable(subjectTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("修改科目信息")
    @PutMapping("/subjectTables")
    public TxnRespResult<SubjectTableRes> modifySubjectTable(@RequestBody SubjectTableReq subjectTableReq) {
        TxnRespResult<SubjectTableRes> result = new TxnRespResult<>();
        SubjectTableRes res;
        subjectTableReq.setUpdateBy(infoService.getUser().getUserName());
        res = iSubjectTableService.modifySubjectTable(subjectTableReq);
        result.getSuccess(res);
        return result;
    }

    @ApiOperation("根据租户ID和科目表ID删除科目信息")
    @DeleteMapping("/subjectTables/{tenantId}/{subjectId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "subjectId", value = "科目表ID", required = true)
    })
    public TxnRespResult<Boolean> removeById(@PathVariable(value = "tenantId") String tenantId, @PathVariable("subjectId") String subjectId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);
            flag = iSubjectTableService.removeByTenIdAndSuubjectId(String.valueOf(infoService.getUser().getProjectId()), subjectId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    @ApiOperation(value = "获得科目信息", notes = "获得科目信息")
    @GetMapping(value = "/subjectTables/spinner")
    public TxnRespResult<ArrayList<SubjectTableRes>> getAllPage() throws TxnException {
        TxnRespResult<ArrayList<SubjectTableRes>> result = new TxnRespResult<>();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        List<SubjectTableRes> allSubject = iSubjectTableService.getAllSubject(tenantId,null);
        return result.getSuccess((ArrayList) allSubject);
    }

    @ApiOperation(value = "获得有效的科目信息", notes = "获得科目信息")
    @GetMapping(value = "/subjectTables/getSubjectIds")
    public TxnRespResult<ArrayList<SubjectTableRes>> getSubjectIds() throws TxnException {
        TxnRespResult<ArrayList<SubjectTableRes>> result = new TxnRespResult<>();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        List<SubjectTableRes> allSubject = iSubjectTableService.getEffectiveSubjectIds(tenantId);
        return result.getSuccess((ArrayList) allSubject);
    }

    @ApiOperation(value = "获取机构下有效的科目信息", notes = "获得科目信息")
    @GetMapping(value = "/subjectTables/orgId/{orgId}")
    public TxnRespResult<ArrayList<SubjectTableRes>> getSubjectIdByOrgId(@PathVariable("orgId") String orgId) throws TxnException {
        TxnRespResult<ArrayList<SubjectTableRes>> result = new TxnRespResult<>();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        List<SubjectTableRes> allSubject = iSubjectTableService.getEffectiveSubjectIdsByOrgId(tenantId,orgId);
        return result.getSuccess((ArrayList) allSubject);
    }
}
